package com.ittest.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ittest.domain.system.Permission;
import com.ittest.factory.MapperFactory;
import com.ittest.mapper.system.PermissionMapper;
import com.ittest.service.system.PermissionService;
import com.ittest.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class PermissionServiceImpl implements PermissionService {

    @Override
    public List<Permission> findAll(int page, int size) {
        SqlSession sqlSession = null;
        List<Permission> list = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper
            PermissionMapper mapper = MapperFactory.getMapper(sqlSession, PermissionMapper.class);
            // 设置分页参数
            PageHelper.startPage(page,size);
            // 调用持久层方法
            list = mapper.findAll();

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public void save(Permission permission) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper
            PermissionMapper mapper = MapperFactory.getMapper(sqlSession, PermissionMapper.class);
            // 添加id
            String id = UUID.randomUUID().toString();
            permission.setId(id);
            // 调用持久层方法
            mapper.save(permission);
            // 提交事务
            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper
            PermissionMapper mapper = MapperFactory.getMapper(sqlSession, PermissionMapper.class);
            // 调用持久层方法
            mapper.delete(id);
            // 提交事务
            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void deleteById(String id) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper
            PermissionMapper mapper = MapperFactory.getMapper(sqlSession, PermissionMapper.class);
            // 切割
            String[] arr = id.split(",");
            // 调用持久层方法
            mapper.deleteById(arr);
            // 提交事务
            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }
}
